package com.wavehi.homework.fifith;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @Author WaveHi
 * @Date 25/8/2021 下午 8:35
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description Fifth homework:Get object from properties file
 */
public class ReflectionDemo5 {

    public static void main(String[] args) {

        //获取类加载器
        ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();

        //获取配置文件的输入流
        InputStream resourceAsStream = contextClassLoader.getResourceAsStream("bean.properties");

        //获取属性集
        Properties properties = new Properties();

        try {
            //加载输入流
            properties.load(resourceAsStream);

            //获取数据
            String className = properties.getProperty("className");

            //获取Class对象
            Class<?> aClass = Class.forName(className);

            //获取对象
            Object o = aClass.newInstance();
            System.out.println(o);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    }

}

//小结：读取properties文件的步骤
//     1.获取类的加载器
//     2.通过类的加载器获取配置文件的输入流
//     3.获取属性集
//     4.加载输入流到属性集中
//     5.通过属性集获取数据。